package com.yiwyn.bytebuddy.proxy.advice;

import net.bytebuddy.asm.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

// 增强逻辑类
public class SayHelloAdvice {

    // 明确指定 log 为 public，避免代理类无法访问 ,@slf4j 创建的log是private的， 在ByteBuddy增强中无法被调用
    public static final Logger log = LoggerFactory.getLogger(SayHelloAdvice.class);

    @Advice.OnMethodEnter
    static void onEnter(@Advice.Origin Method method) {
        log.info("[{}]访问开始", method.getName());
    }

    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
    static void onExit(@Advice.Return(readOnly = true) String result, @Advice.Origin Method method, @Advice.Thrown Throwable throwable) {
        if (throwable != null) {
            log.error("[{}]方法调用异常", method.getName(), throwable);
        } else {
            log.info("[{}]访问结束 返回值:{}", method.getName(), result);
        }
    }
}